package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.base.FeedbackMapper;
import com.weipai.demo.model.Feedback;
import com.weipai.demo.service.FeedbackService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/17 15:06
 * @Version 1.0
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FeedbackMapper feedbackMapper;

    //添加意见
    @Override
    public Response insert(Feedback feedback) {
        Response response = new Response();
        try {
            if (!feedback.equals("") || feedback != null) {
                feedback.setReveal(1);

                int insert = feedbackMapper.insertSelective(feedback);
                response.setCode(RetCode.SUBMITSUCCESS);
            } else {
                response.setCode(RetCode.FEEDBACKNOTNULL);
                return response;
            }
        } catch (Exception e) {
           throw new RuntimeException(e.getMessage());
        }
        return response;
    }

    //======================================================================

    //分页显示意见反馈
    @Override
    public LayuiTablePagingResult showFeedBack(Integer page, Integer size, Feedback feedback) {


        LayuiTablePagingResult result = null;
        try {
            PageHelper.startPage(page,size);
            List<Feedback> newsList = feedbackMapper.select(feedback);
            PageInfo<Feedback> pageInfo=new PageInfo<>(newsList);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return result;
    }

    //删除意见
    @Override
    public Response updateView(Integer id) {
        Response response=new Response();
        try {
            Feedback feedback=new Feedback();
            feedback.setId(id);
            int delete = feedbackMapper.delete(feedback);
            response.setCode(RetCode.DELETESUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(RetCode.REQUEST_ERROR);
            logger.error("updateView"+e.getMessage());
        }
        return response;

    }


}


